package com.example.Nurse_Notifier;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import interfaces.ChangeState;
import interfaces.NotifyUser;

public class HistFragment extends Fragment implements ChangeState, NotifyUser {

    ListView histListView;
    ArrayAdapter<HistString> histAdapter;
    HistContainer histContainer;
    FragmentActivity activity;
    boolean pressedReceived = false;
    Typeface regular;
//    Runnable runnable = new Runnable() {
//        @Override
//        public void run() {
//            histAdapter.notifyDataSetChanged();
//        }
//    };


    public HistFragment(HistContainer histContainer, FragmentActivity activity) {
        this.activity = activity;
        this.histContainer = histContainer;
        histAdapter = new HistArrayAdapter(activity, histContainer);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.historylayout, container, false);
        histListView = (ListView) rootView.findViewById(R.id.histListView);
        histListView.setAdapter(histAdapter);
        regular = Typeface.createFromAsset(activity.getAssets(), "Roboto-Bold.ttf");
        TextView textView = (TextView) rootView.findViewById(R.id.pretendingTextView);
        textView.setTypeface(regular);
        return rootView;
    }

    @Override
    public void addObject(final XbeeDevice object) {
//
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
        histContainer.add(new HistString(activity.getApplicationContext(), histContainer.size(), object.getId() + 1,
                HistString.DEVICE_ADDED));
        histAdapter.notifyDataSetChanged();
//            }
//        });
    }

    @Override
    public void removeObject(final int devNum) {

//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
        histContainer.add(new HistString(activity.getApplicationContext(), histContainer.size(), devNum + 1,
                HistString.DEVICE_REMOVED));
        histAdapter.notifyDataSetChanged();
//            }
//        });
    }

    @Override
    public void changeObjectStatus(final int objectId, final int status) {
//
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
        histContainer.add(new HistString(activity.getApplicationContext(), histContainer.size(), objectId + 1,
                status));
        histAdapter.notifyDataSetChanged();
//            }
//        });
    }

    @Override
    public void notifyButtonPressed(final int devNum, final boolean isPressed) {
        if (isPressed && !pressedReceived) {
            pressedReceived = true;
//            activity.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
            histContainer.add(new HistString(activity.getApplicationContext(), histContainer.size(), devNum + 1,
                    HistString.DEVICE_BUTTON_PRESSED));
            histAdapter.notifyDataSetChanged();
//                }
//            });
        } else if (!isPressed && pressedReceived) {
            pressedReceived = false;

//            activity.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
            histContainer.add(new HistString(activity.getApplicationContext(), histContainer.size(), devNum + 1,
                    HistString.DEVICE_BUTTON_UNPRESSED));
            histAdapter.notifyDataSetChanged();
//                }
//            });
        }
    }

    @Override
    public void notifyAlarm(int devNum) {

    }

    public void clearHistory() {

    }
}